from django.shortcuts import render, redirect
from .models import NovelCategory, Novel, NovelChapter
from get_novel import search_novels
from tasks import get_novel_base_info
import time

def index(request):
    if request.method == 'GET':
        novel_category_objs = NovelCategory.objects.all()
        return render(request, 'demo/index.html', context={"novel_category_objs": novel_category_objs})

def novel_list(request):
    if request.method == 'GET':
        novel_category_objs = NovelCategory.objects.all()
        cat_id = request.GET.get('cid', False)
        novel_objs = Novel.objects.filter(novel_category_id=cat_id)
        context = {"novel_category_objs": novel_category_objs, "novel_objs": novel_objs}
        return render(request, 'demo/novel_category_list.html', context=context)

def search_list(request):
    if request.method == 'GET':
        # 1 获取小说分类
        novel_category_objs = NovelCategory.objects.all()
        # 2 获取搜索小说的列表
        search_novel_list = []
        search_name = request.GET.get('s', False)
        if search_name:
            search_novel_list = search_novels(novel_name=search_name)
        # 2.1 判断小说是否在本站存在
        search_novel_list_after = []
        novel_objs_had = Novel.objects.filter(name__icontains=search_name)
        for search_novel in search_novel_list:
            search_novel['is_exists'] = False
            for novel_obj in novel_objs_had:
                if novel_obj.url == search_novel['url']:
                    search_novel['is_exists'] = True
            search_novel_list_after.append(search_novel)

        # 3 组装上下文参数
        context = {"novel_category_objs": novel_category_objs,
                   'search_novel_list': search_novel_list_after}
        return render(request, 'demo/search_list.html', context=context)

def get_novel(request):
    if request.method == 'GET':
        nurl = request.GET.get('nurl', False)
        print('nurl: ', nurl)
        get_novel_base_info.delay(nurl=nurl)
        # time.sleep(10)
        return redirect('/index/')


def novel_index(request):
    if request.method == 'GET':
        # 1 获取小说分类
        novel_category_objs = NovelCategory.objects.all()
        # 2 获取搜索小说的列表
        nurl = request.GET.get('nurl', False)
        novel_obj = Novel.objects.get(url=nurl)
        # 2.1 获取小说的章节列表
        chapter_objs = NovelChapter.objects.filter(novel_id= novel_obj.id)
        # 3 组装上下文参数
        context = {"novel_category_objs": novel_category_objs,
                   'novel_obj': novel_obj,
                   'chapter_objs': chapter_objs}
        return render(request, 'demo/novel_index.html', context=context)


def novel_chapter(request):
    if request.method == 'GET':
        # 1 获取小说分类
        novel_category_objs = NovelCategory.objects.all()
        # 2 获取章节信息
        chapter_id = request.GET.get('id', 0)
        chapter_obj = NovelChapter.objects.get(id=chapter_id)

        # 3 组装上下文参数
        context = {"novel_category_objs": novel_category_objs,
                   'chapter_obj': chapter_obj}
        return render(request, 'demo/novel_chapter.html', context=context)
